package com.mh.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mh.shiro.business.entity.User;
import com.mh.shiro.business.enums.UserStatusEnum;
import com.mh.shiro.business.service.SysUserService;
import com.mh.shiro.business.vo.UserConditionVO;
import com.mh.shiro.framework.exception.MhException;
import com.mh.shiro.framework.holder.RequestHolder;
import com.mh.shiro.persistence.beans.SysUser;
import com.mh.shiro.persistence.mapper.SysUserMapper;
import com.mh.shiro.util.IpUtil;
import com.mh.shiro.util.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年06月21日13:55
 * @Description: <描述>
 */
@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<User> findPageBreakByCondition(UserConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<SysUser> sysUsers = sysUserMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(sysUsers)) {
            return null;
        }
        List<User> users = new ArrayList<>();
        for (SysUser su : sysUsers) {
            users.add(new User(su));
        }
        PageInfo bean = new PageInfo<SysUser>(sysUsers);

        bean.setList(users);

        return bean;
    }

    @Override
    public User getByUserName(String userName) {
        User user = new User(userName, null);

        return getOneByEntity(user);
    }

    @Override
    public User updateUserLastLoginInfo(User user) {
        if(user != null) {
            user.setLoginCount(user.getLoginCount() + 1);
            user.setLastLoginTime(new Date());
            user.setLastLoginIp(IpUtil.getRealIp(RequestHolder.getRequest()));
            user.setPassword(null);

            this.updateSelective(user);
        }
        return user;
    }

    @Override
    public List<User> listByRoleId(Long roleId) {
        List<SysUser> sysUsers = sysUserMapper.listByRoleId(roleId);
        if(CollectionUtils.isEmpty(sysUsers)) {
            return null;
        }
        List<User> users = new ArrayList<>();

        for(SysUser su : sysUsers) {
            users.add(new User(su));
        }
        return users;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public User insert(User user) {
        Assert.notNull(user, "User不可为空");
        user.setUpdateTime(new Date());
        user.setCreateTime(new Date());
        //设置ip
        user.setRegIp(IpUtil.getRealIp(RequestHolder.getRequest()));
        //设置用户状态
        user.setStatus(UserStatusEnum.NORMAL.getCode());

        sysUserMapper.insertSelective(user.getSysUser());
        return user;
    }

    @Override
    public void insertList(List<User> entities) {

    }

    @Override
    public boolean removeByPrimaryKey(Long primaryKey) {
        return sysUserMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    public boolean update(User entity) {
        return false;
    }

    @Override
    public boolean updateSelective(User user) {
        Assert.notNull(user, "User不可为空");
        user.setUpdateTime(new Date());
        if (!StringUtils.isEmpty(user.getPassword())) {
            try {
                user.setPassword(PasswordUtil.encrypt(user.getPassword(),user.getUsername()));
            } catch (Exception e) {
                throw new MhException("密码加密失败");
            }
        } else {
            user.setPassword(null);
        }
        return sysUserMapper.updateByPrimaryKeySelective(user.getSysUser()) > 0;
    }

    @Override
    public User getByPrimaryKey(Long primaryKey) {
        Assert.notNull(primaryKey, "PrimaryKey不可为空");
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(primaryKey);

        return null == sysUser ? null : new User(sysUser);
    }

    @Override
    public User getOneByEntity(User entity) {
        Assert.notNull(entity, "User不可为空！");

        SysUser sysUser = sysUserMapper.selectOne(entity.getSysUser());

        return null == sysUser ? null : new User(sysUser);
    }

    @Override
    public List<User> listAll() {
        return null;
    }

    @Override
    public List<User> listByEntity(User entity) {
        return null;
    }
}
